import axios from 'axios'
import {Message} from "element-ui";
import {getToken} from '@/utils/auth'
// export let isRelogin = { show: false };

// axios.defaults.headers['Content-Type'] = 'application/json;charset=utf-8'

// 创建axios实例
const service = axios.create({
    // axios中请求配置有baseURL选项，表示请求URL公共部分
    baseURL: 'https://hubujyjs.cn:8080/api',
    // baseURL: 'http://localhost:8000/api',
    // 超时
    timeout: 5000
})
//请求拦截器
service.interceptors.request.use(
    config => {
        config.headers = {
            "Content-Type": "application/json" //配置请求头
        };

        const token=getToken()
        if (token){
            config.headers['X-Token'] =token
        }
        return config
    },
    error => {
        // do something with request error
        // Message.error({message: '连接不到服务器，请稍后再试', offset: 150});
        console.log(error) // for debug
        return Promise.reject(error)
    }

)
// 响应拦截器
service.interceptors.response.use(res=> {
    const code = res.data.code
    const msg = res.data.msg
    // 二进制数据则直接返回
    if(res.request.responseType ===  'blob' || res.request.responseType ===  'arraybuffer'){
        return res.data
    }
    if (code===200){
        return res.data
    } else {
        Message({
            message: msg,
            type: "error"
        })
        return Promise.reject('error')
    }
});


export default service
